--
-- Priest Quest v1.10
--
-- This Addon is only usefull to get a lot of Help
-- with the Priest Quest for Anathema // Benediktion.
--
-- This Addon is Updated for Patch 1.10 by Nitram @ De-Azshara
-- and pushed up to a little more usualy.
--
-- This Addon v1.00 original is written by Adranar <Might> from Lothar
--
-- Changes:
--
-- v1.00: release from Adranar
-- v1.10: changed ToolTipbugs with wow 1.10,
--        added some Debugging Stuff
--        many other little Updates
--
-- Howto Use:
--
-- This tool just add some improfements so that you only have to select
-- a NPC. The Easyest way to get all into the finish is repeatedly pushing
-- the "Target Nearst Friend" Button. Each targetchange starts a check
-- if heal or Abolish Disease is need and cast the best Spell. It includes
-- some Mana save spell Selections so that priests without epic gear can
-- simply get Benediction/Anathema. Good Luck!
--
-- PS: The finest way to get Benediction is to do it by your own. Nothing is
--     better than get this realy great Quest finished without Addons who
--     simplify the Quest.
--

PriestQuest_UsePriestQuest = false;
PriestQuest_HasDisease = false;
PriestQuest_HasAbolish = false;
PriestQuest_HasRenew = false;
PriestQuest_Debug = true;

if (GetLocale() == "deDE") then
	DISEASE		= "Schwelle des Todes";
	RENEW    	= "Erneuerung";
	RENEW9    	= "Erneuerung(Rang 9)";
	RENEW8    	= "Erneuerung(Rang 8)";
	RENEW7    	= "Erneuerung(Rang 7)";
	CURE      	= "Krankheit aufheben";
	FHEAL		= "Blitzheilung(Rang 4)";
else
	DISEASE		= "疾病";
	RENEW    	= "恢复";
	RENEW9    	= "恢复(等级 9)";
	RENEW8    	= "恢复(等级 8)";
	RENEW7    	= "恢复(等级 7)";
	CURE      	= "驱除疾病";
	FHEAL		= "快速治疗(等级 4)";
end

function PriestQuest_OnLoad()
	tinsert(UISpecialFrames, "PriestQuestFrame");
	SLASH_PRIESTQUEST1 = "/PriestQuest";
	SLASH_PRIESTQUEST2 = "/PQ";
	SlashCmdList["PRIESTQUEST"] = function(msg)
		PriestQuest_SlashCommand(msg);
	end
end

function PriestQuest_Debug( Message )
	if( PriestQuest_Debug == true ) then
		DEFAULT_CHAT_FRAME:AddMessage(Message);
	end
end

function PriestQuest_CheckForBuffs()
	PriestQuest_HasAbolish = false;
	PriestQuest_HasRenew = false;

	PriestQuest_Debug( "PQ: 检查debuffs." );

	local i = 1;
  	while UnitBuff("target", i) do
		PriestQuestTooltip:ClearLines();
		PriestQuestTooltip:SetUnitBuff("target", i);
--		local pq_buff_names = PriestQuestTooltipTextRight1:GetText();
		local pq_buff_names = PriestQuestTooltipTextLeft1:GetText();

		if ( pq_buff_names == nil) then
			PriestQuest_Debug( "PQ: "..i..": 未发现Buff!");
			-- break;
		else
			PriestQuest_Debug( "PQ: "..i..": "..pq_buff_names.." 发现!");

			if string.find( pq_buff_names, RENEW) then
    				PriestQuest_HasRenew = true;
				PriestQuest_Debug( "PQ: "..i..": 有恢复 = true" );
	    		end

	    		if string.find( pq_buff_names, CURE) then
		    		PriestQuest_HasAbolish = true;
				PriestQuest_Debug( "PQ: "..i..": 有驱病 = true" );
    			end

		end
		PriestQuest_Debug( "PQ: "..i..": Stepende" );
    	i = i + 1;
  	end
end

function PriestQuest_CheckForDebuffs()
	PriestQuest_HasDisease = false;

	PriestQuest_Debug( "PQ: 检查debuffs." );
	local i = 1;
  	while UnitDebuff("target", i) do
		PriestQuestTooltip:ClearLines();
		PriestQuestTooltip:SetUnitDebuff("target",i);

--		local pq_debuff_names = PriestQuestTooltipTextRight1:GetText();
	  local pq_debuff_names = PriestQuestTooltipTextLeft1:GetText();

		if ( pq_debuff_names == nil) then
			PriestQuest_Debug( "PQ: "..i..": 未发现Debuff!");
--			break;
		else
			PriestQuest_Debug( pq_debuff_names.." 发现!");

			if  string.find( pq_debuff_names, DISEASE)  then
    				PriestQuest_HasDisease = true;
				PriestQuest_Debug( "PQ: "..i..": 有疾病 = true" );
      				break;
		    	end
		end

    	i = i + 1;
  	end
end

function PriestQuest_OnEvent()
	if (event == "PLAYER_TARGET_CHANGED") then
		if (UnitIsFriend("player", "target")) then
			PriestQuest_CheckForBuffs();
			PriestQuest_CheckForDebuffs();

			if ( PriestQuest_HasDisease and not PriestQuest_HasAbolish ) then
			  PriestQuest_Debug ("Curing");
			  CastSpellByName(CURE);
			else
				if ( UnitHealth("target") < 80 and UnitHealth("target") > 49 and not PriestQuest_HasRenew ) then
					CastSpellByName(RENEW7);
				end

				if ( UnitHealth("target") < 50 and UnitHealth("target") > 29 and not PriestQuest_HasRenew ) then
					CastSpellByName(RENEW8);
				end

				if ( UnitHealth("target") < 30 and not PriestQuest_HasRenew ) then
					CastSpellByName(RENEW9);
				end

				if ( UnitHealth("target") < 20 and PriestQuest_HasRenew ) then
					CastSpellByName(FHEAL);
				end
			end
		end
	end
end

function PriestQuest_SlashCommand(msg)
	if(PriestQuest_UsePriestQuest ) then
		PriestQuest_UsePriestQuest = false;
		PriestQuestFrame:UnregisterEvent("PLAYER_TARGET_CHANGED");
		DEFAULT_CHAT_FRAME:AddMessage("Priest Quest 启用");
	else
		PriestQuest_UsePriestQuest = true;
		PriestQuestFrame:RegisterEvent("PLAYER_TARGET_CHANGED");
		DEFAULT_CHAT_FRAME:AddMessage("Priest Quest 禁用");
	end
end
